package com.daigoudao.web.model;

import java.io.Serializable;
import java.math.BigDecimal;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.ImageUtil;

/**
 * 购物车项
 * 
 * @author zheng
 * 
 */
public class CartItem implements Serializable {

	private static final long serialVersionUID = -7213347410684178873L;

	private int brokerId;

	private int productId;

	private String productName;

	private String image;

	private double price;

	private double serviceFee;

	private Logistics logistics;

	/**
	 * 购买数量
	 */
	private int number;

	public CartItem() {

	}

	public CartItem(CanBuyModel product, int number) {
		this.number = number;
		this.price = product.getPrice();
		this.serviceFee = product.getServiceFee();
		if (product.getImages() != null && product.getImages().size() > 0) {
			this.image = product.getImages().get(0);
		}
		this.productName = product.getGoodsName();
		this.logistics = product.getLogistics();
		this.brokerId = product.getUser().getUserid();
		this.productId = product.getCanId();
	}

	/**
	 * 更新商品基本信息
	 * 
	 * @param product
	 */
	public void changeItem(CanBuyModel product) {
		this.productName = product.getGoodsName();
		this.logistics = product.getLogistics();
		this.price = product.getPrice();
		this.serviceFee = product.getServiceFee();
		if (product.getImages() != null && product.getImages().size() > 0) {
			this.image = product.getImages().get(0);
		}
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public void addNumber(int number) {
		this.number += number;
	}

	/**
	 * 小计
	 * 
	 * @return
	 */
	public float getSubtotal() {
		return new BigDecimal((price + serviceFee) * number).setScale(2,
				BigDecimal.ROUND_HALF_UP).floatValue();
	}

	public int getProductId() {
		return productId;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public double getPrice() {
		return BigDecimal.valueOf(price).setScale(2, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getServiceFee() {
		return BigDecimal.valueOf(serviceFee)
				.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	public void setServiceFee(double serviceFee) {
		this.serviceFee = serviceFee;
	}

	public Logistics getLogistics() {
		return logistics;
	}

	public void setLogistics(Logistics logistics) {
		this.logistics = logistics;
	}

	public int getBrokerId() {
		return brokerId;
	}

	public void setBrokerId(int brokerId) {
		this.brokerId = brokerId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getAbsoluteImage() {
		if (image != null) {
			String imagePath = ImageUtil.getImageFileName(image, 50);
			return ConfigManager.getConfigString("upload.resUrl") + imagePath;
		}
		return image;
	}
}
